<?php

require '../inc/init.php';
require '../inc/socket.class.php';
chkpurview('activity_list_mange');

$act = isset($_GET['act']) ? $_GET['act'] : NULL;

if($act == 'open')			//开启活动
{
	$actId = isset($_GET['actid'])?$_GET['actid']:0;
	$sql = "SELECT id as act_id,platform_id,server_id,act_name,act_content,act_interval,start_date,award_date,finish_date,act_info,actgift_info 
				FROM tb_ad_activity
				WHERE id = $actId";
	$act_temp 	= $db->getRow($sql);
	
	$platform_id 	= unserialize($act_temp['platform_id']);
	$server_id 		= unserialize($act_temp['server_id']);
	
	$action 			= isset($_GET['action'])?$_GET['action']:0;	//否需要删除当前活动领取过的奖励 1：删除；0：不删除
	$act_id				= $act_temp['act_id'];
	
	$act_name 		= $act_temp['act_name'];
	
	$act_content	= urldecode(stripslashes($act_temp['act_content']));
	
	$linkArray = array();
	$contentArray = array();
	$tmp = @match_links($act_content);						//提取邮件内容中的链接
	foreach($tmp['content'] as $key=>$val)				//将链接替换成html格式
	{
		$act_content = str_replace($val, "<FONT COLOR='#00FF00'><A HREF='".$tmp['link'][$key]."' TARGET='_blank'><U>".$val."</U></A></FONT>", $act_content);
	}
	$act_content = str_replace('<span style="color:', '<font color="', $act_content);
	$act_content = str_replace('</span>', '</font>', $act_content);
	
	$act_interval	= $act_temp['act_interval'];
	$start_date		= strtotime($act_temp['start_date']);
	$award_time 	= strtotime($act_temp['award_date']);
	$finish_date	= strtotime($act_temp['finish_date']);
	$act_info			= $act_temp['act_info'];
	$actgift_info	= unserialize($act_temp['actgift_info']);
	
	//解析礼包，生成字符串
	//$gift1 = array(1,'1066:10:1;1066:10:1;1066:10:1;','1:100;2:100');
	//$gift2 = array(1,'1066:10:1;1066:10:1;1066:10:1;','1:100;2:100');
	$gift_info = array();
	$item = array();
	foreach ($actgift_info as $k=>$v)
	{
		//礼品字符串
		foreach($v['item'] as $k1=>$v1)
		{
			$item[$k] .= $v1['id'].':'.$v1['num'].':'.$v1['property'].';';
		}
		
		//礼包规则字符串
		if(!empty($v['charge_num']))
		{
			$rule[$k] .= '1:'.$v['charge_num'].';';
		}
		
		if(!empty($v['consume_num']))
		{
			$rule[$k] .= '2:'.$v['consume_num'].';';
		}
		
		if(!empty($v['charge_one_num']))
		{
			$rule[$k] .= '3:'.$v['charge_one_num'].';';
		}
		
		if(!empty($v['package_type']))
		{
			$rule[$k] .= '4:'.$v['package_type'].';';
		}
		
		if(!empty($v['online_time']))
		{
			$rule[$k] .= '5:'.$v['online_time'].';';
		}
		
		if(!empty($v['vip_level']))
		{
			$rule[$k] .= '6:'.$v['vip_level'].';';
		}
		
		if(!empty($v['continues_login']))
		{
			$rule[$k] .= '7:'.$v['continues_login'].';';
		}
		
		if(!empty($v['continues_logoff']))
		{
			$rule[$k] .= '8:'.$v['continues_logoff'].';';
		}
		
		if(!empty($v['receive_rose']))
		{
			$rule[$k] .= '9:'.$v['receive_rose'].';';
		}
		
		if(!empty($v['rank_type']))
		{
			$rule[$k] .= '4097:'.$v['rank_type'].':'.$v['rank_index_min'].':'.$v['rank_index_max']. ';';
		}
		
		if(!empty($v['system_function']))
		{
			$rule[$k] .= '4098:'.$v['system_function'].':'.$v['system_times'].';';  
		}
		
		if(!empty($v['profession_limit_zhanshi']) || !empty($v['profession_limit_fashi']) || !empty($v['profession_limit_gongshou']))
		{
			$rule[$k] .= '4099:'.($v['profession_limit_zhanshi']+$v['profession_limit_fashi']+$v['profession_limit_gongshou']).';';
		}
		
		if(!empty($v['level_min_limit']))
		{
			$rule[$k] .= '4100:'.$v['level_min_limit'].':'.$v['level_max_limit'].';';
		}
		
		if(!empty($v['bang_level']))
		{
			$rule[$k] .= '4101:'.$v['bang_level'].';';
		}
		
		if(!empty($v['bang_bloom']))
		{
			$rule[$k] .= '4102:'.$v['bang_bloom'].';';
		}
		
		if(!empty($v['friend_times']))
		{
			$rule[$k] .= '4103:'.$v['friend_times'].';';
		}
		
		if(!empty($v['sex_limit']))
		{
			$rule[$k] .= '4104:'.$v['sex_limit'].';';
		}
		
		$gift_info[$k] = array($k,$item[$k],$rule[$k]);
	}
	
	
	$params['act'] = $act;
	$params['action'] = $action;
	$params['act_id'] = $act_id;
	$params['start_date'] = $start_date;
	$params['finish_date'] = $finish_date;
	$params['award_time'] = $award_time;
	$params['act_name'] = $act_name;
	$params['act_content'] = $act_content;
	$params['act_info'] = $act_info;
	$params['actgift_info'] = json_encode($gift_info);

	//逐个单服发送
	$msg = array();
	$i = 0;
	foreach($server_id as $k=>$v)
	{
		//**************************************************
//		$sendData = array(
//							array('type'=>1,'data'=>$action),
//							array('type'=>2,'data'=>$act_id),
//							array('type'=>2,'data'=>$start_date),
//							array('type'=>2,'data'=>$finish_date),
//							array('type'=>2,'data'=>$award_time),
//							array('type'=>3,'data'=>$act_name),
//							array('type'=>3,'data'=>$act_content),
//							array('type'=>3,'data'=>$act_info),			//活动设置
//							array('type'=>9,'data'=>$gift_info),		//活动奖励物品
//					);
//						
//		$protocol = 'addactivity';
//		
//		$socket_gateway['address']='192.168.10.86';
//		$socket_gateway['port']='8013';
//		$socket_gateway['address']='192.168.8.199';
//		$socket_gateway['port']='8033';
//		$gamesocket = new gamesocket();
//		$receive = $gamesocket->send($sendData, $protocol);
//		if($receive['retcode']=='00000000')
//		{  
//			echo 1;
//		}
//		else 
//		{
//			echo 0;
//		}
//		exit;
		//********************************************
		
		
		$send_url = 'https://' . getServerById($v) . '/task/activity_api.php';
		$retcode = makeRequest($send_url, $params);
		if($retcode == '1')
		{
			act_open_status('open',$act_id);
			$msg[$i]['server'] = getServerNameById($v);
			$msg[$i]['status'] = 1;		//成功
		}
		else			
		{
			$msg[$i]['server'] = getServerNameById($v);
			$msg[$i]['status'] = 0;		//失败
			$msg[$i]['serverId'] = $v;
		}
		$i++;
	}
	echo json_encode($msg); 
	exit;
}
elseif($act == 'stop')	//停止活动
{
	$actId = isset($_GET['actid'])?$_GET['actid']:0;
	$sql = "SELECT id as act_id,platform_id,server_id,action,act_name,act_content,act_interval,start_date,award_date,finish_date,act_info,actgift_info 
				FROM tb_ad_activity
				WHERE id = $actId";
	$act_temp 	= $db->getRow($sql);
	
	$platform_id 	= unserialize($act_temp['platform_id']);
	$server_id 		= unserialize($act_temp['server_id']);
	
	$params['act'] = $act;
	$params['act_id'] = $actId;
	
	//逐个单服发送
	$msg = array();
	$i = 0;
	foreach($server_id as $k=>$v)
	{
		$send_url = 'https://' . getServerById($v) . '/task/activity_api.php';
		$retcode = makeRequest($send_url, $params);
		if($retcode == '1')
		{
			act_open_status('stop',$actId);
			$msg[$i]['server'] = getServerNameById($v);
			$msg[$i]['status'] = 1;		//成功
		}
		elseif($retcode == '-1') 
		{
			$msg[$i]['server'] = getServerNameById($v);
			$msg[$i]['status'] = -1;		//已删除
			$msg[$i]['serverId'] = $v;
		}
		else
		{
			$msg[$i]['server'] = getServerNameById($v);
			$msg[$i]['status'] = 0;		//失败
			$msg[$i]['serverId'] = $v;
		}
		$i++;
	}
	echo json_encode($msg); 
	exit;
}
elseif($act == 'delete')		//本地删除
{
	$actId 	= isset($_GET['actid']) ? $_GET['actid'] : 0;
	$sql = "DELETE FROM tb_ad_activity WHERE id = $actId";
	$isDeleted = $db->query($sql);
	$isDeleted ? exit('1') : exit('0');
}

//如果活动到期时间小于当前时间，则在央服将该活动状态设置为0
$sql = "SELECT  id,finish_date 	FROM tb_ad_activity";
$activitys = $db->getAll($sql);
foreach($activitys as $k=>$v)
{
	if($v['finish_date']<date('Y-m-d H:i:s',time()))
	{
		act_open_status('stop',$v['id']);
	}
}

$page = isset($_GET['page']) && $_GET['page'] >= 1 ? $_GET['page'] : 1;
$pagesize = 12;
$activitys = array();
//已增加活动列表
$sql = "SELECT platform_id,server_id,action,
						 id as act_id,act_name,act_content,
						 `act_interval`,start_date,award_date,finish_date,
						  act_info,actgift_info,status 
			FROM tb_ad_activity
			order by id desc";
$activitys = $db->getAll($sql,$page,$pagesize);
$count = $db->getOne('select count(*) from tb_ad_activity');

foreach($activitys as $k=>$v)
{
	$activitys[$k]['platform_id']='腾讯';
	$activitys[$k]['server_id'] = explainServerName(unserialize($v['server_id']),false);
	$activitys[$k]['act_content']=stripslashes($v['act_content']);
}

$T->assign('page',showpage($page, $pagesize, $count, $url='?'));
$T->assign('activitys',$activitys);
$T->display('activity_list.html');

/**
 * 记录活动是否开启状态
 * @param unknown_type $actid
 */
function act_open_status($operate,$actid)
{
	global $db;
	if($operate == 'open')		//活动已经开启
	{
		$sql = "UPDATE tb_ad_activity SET STATUS = 1 WHERE id =  $actid";
	}
	else 			//活动停止
	{
		$sql = "UPDATE tb_ad_activity SET STATUS = 0 WHERE id =  $actid";
	}
	$isOpened = $db->query($sql);
	if($isOpened)
	{
		return TRUE;
	} 
	else 
	{
		return false;
	}
}


